﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using AIStudio.Core.Models;
using AIStudio.Core.ViewModels;
using AIStudio.Wpf.Base_Manage.Views;
using AIStudio.Wpf.Business.IBussiness;
using AIStudio.Wpf.Entity.Models.Models;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;

namespace AIStudio.Wpf.Base_Manage.ViewModels
{
    public class Base_AppSecretViewModel : TitleViewModel
    {
        protected IDataProvider _dataProvider { get; }

        public Base_AppSecretViewModel(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            GetData();
        }

        protected string Area { get; set; } = "Base_Manage";

        protected string GetDataList { get; set; }

        protected Type Type { get; set; }
        protected Type EditType { get; set; }

        private ObservableCollection<Base_AppSecret> _data;
        public ObservableCollection<Base_AppSecret> Data
        {
            get { return _data; }
            set
            {
                SetProperty(ref _data, value);
            }
        }


        private Base_AppSecret _selectedItem;
        public Base_AppSecret SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                SetProperty(ref _selectedItem, value);
            }
        }

        private ICommand _addCommand;
        public ICommand AddCommand
        {
            get
            {
                return this._addCommand ?? (this._addCommand = new DelegateCommand(() => this.Edit()));
            }
        }

        private ICommand _editCommand;
        public ICommand EditCommand
        {
            get
            {
                return this._editCommand ?? (this._editCommand = new DelegateCommand<Base_AppSecret>(para => this.Edit(para)));
            }
        }

        private ICommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                return this._deleteCommand ?? (this._deleteCommand = new DelegateCommand(() => this.Delete()));
            }
        }


        private ICommand _deleteOneCommand;
        public ICommand DeleteOneCommand
        {
            get
            {
                return this._deleteOneCommand ?? (this._deleteOneCommand = new DelegateCommand<string>(para => this.Delete(para)));
            }
        }

        private ICommand _refreshCommand;
        public ICommand RefreshCommand
        {
            get
            {
                return this._refreshCommand ?? (this._refreshCommand = new DelegateCommand(() => this.Search()));
            }
        }


        protected async void GetData(bool iswaiting = false)
        {
            try
            {
                var result = await _dataProvider.GetData<List<Base_AppSecret>>($"/{Area}/{typeof(Base_AppSecret).Name}/GetDataList", "{}");
                if (!result.Success)
                {
                    throw new Exception(result.Msg);
                }
                else
                {
                    Data = new ObservableCollection<Base_AppSecret>(result.Data);
                    if (Data.Any())
                    {
                        SelectedItem = Data.FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected async void Edit(Base_AppSecret para = null)
        {
            var viewmodel = new Base_AppSecretEditViewModel(para, Area);
            //let's set up a little MVVM, cos that's what the cool kids are doing:
            var view = new Base_AppSecretEdit
            {
                DataContext = viewmodel
            };

            //show the dialog
            var res = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);

            if (object.Equals(res, true))
            {
                try
                {
                    var result = await _dataProvider.GetData<AjaxResult>("/Base_Manage/Base_AppSecret/SaveData", JsonConvert.SerializeObject(viewmodel.Data));
                    if (!result.Success)
                    {
                        throw new Exception(result.Msg);
                    }
                    GetData(true);
                }
                catch (Exception ex)
                {
                    
                }
            }
        }

        private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
            => Debug.WriteLine("You can intercept the closing event, and cancel here.");
        protected async void Delete(string id = null)
        {
            List<string> ids = new List<string>();
            ids.Add(id);


            var sure = MessageBox.Show("确认删除吗?", "删除", MessageBoxButton.YesNo);
            if (sure == MessageBoxResult.Yes)
            {
                try
                {
                    var result = await _dataProvider.GetData<AjaxResult>($"/{Area}/{typeof(Base_AppSecret).Name}/DeleteData", JsonConvert.SerializeObject(ids));
                    if (!result.Success)
                    {
                        throw new Exception(result.Msg);
                    }
                    GetData(true);
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void Search(object para = null)
        {
            GetData();
        }
    }
}
